package com.tianma.web.controller.tianma.file;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.tianma.common.core.domain.entity.SysUser;
import com.tianma.common.utils.SecurityUtils;
import com.tianma.common.utils.StringUtils;
import com.tianma.system.mapper.SysUserMapper;
import com.tianma.web.domain.master.file.WeekEventDetail;
import com.tianma.web.domain.vo.WeekEventVo;
import com.tianma.web.mapper.master.WeekEventDetailMapper;
import com.tianma.web.service.master.IWeekEventDetailService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.tianma.common.annotation.Log;
import com.tianma.common.core.controller.BaseController;
import com.tianma.common.core.domain.AjaxResult;
import com.tianma.common.enums.BusinessType;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import com.tianma.web.domain.master.file.WeekEvent;
import com.tianma.web.service.master.IWeekEventService;
import com.tianma.common.utils.poi.ExcelUtil;
import com.tianma.common.core.page.TableDataInfo;

/**
 * 事件记录看版Controller
 * 
 * @author wwf
 * @date 2025-05-20
 */
@RestController
@RequestMapping("/weekEvent/event")
public class WeekEventController extends BaseController
{
    @Autowired
    private IWeekEventService weekEventService;
    @Autowired
    private IWeekEventDetailService weekEventDetailService;
    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询事件记录看版列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WeekEventVo condition)
    {
        Boolean watchMyself = condition.getWatchMyself();
        //只看自己
        if(watchMyself) {
            String nickname = SecurityUtils.getNickname();
            condition.setCreateUser(nickname);
        }
        startPage();
        List<WeekEvent> list = weekEventService.selectWeekEventListByLike(condition);
        List<WeekEventVo> weekEventVos = new ArrayList<>();
        //处理下一节点处理人信息
        for (WeekEvent item : list){
            WeekEventVo weekEventVo = new WeekEventVo();
            BeanUtils.copyProperties(item, weekEventVo);
            String processUser = item.getProcessUser();
            weekEventVo.setNextUserName(processUser);
            if(StringUtils.isNotEmpty(processUser)) {
                String nickName = weekEventDetailService.processUserHandler(processUser);
                weekEventVo.setNextNickName(nickName);
            }
            weekEventVos.add(weekEventVo);
        }
        return getDataTable(weekEventVos);
    }

    /**
     * 导出事件记录看版列表
     */
    @Log(title = "事件记录看版", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WeekEvent weekEvent)
    {
        List<WeekEvent> list = weekEventService.selectWeekEventList(weekEvent);
        ExcelUtil<WeekEvent> util = new ExcelUtil<WeekEvent>(WeekEvent.class);
        util.exportExcel(response, list, "事件记录看版数据");
    }

    /**
     * 获取事件记录看版详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return AjaxResult.success(weekEventService.selectWeekEventInfo(id));
    }

    /**
     * 新增事件记录看版
     */
    @Log(title = "事件记录看版", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody WeekEventVo weekEventVo)
    {
        weekEventService.publicEventData(weekEventVo);
        return toAjax(1);
    }


    /**
     * 修改事件记录看版
     */
    @Log(title = "事件记录看版", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody WeekEventVo weekEventVo)
    {
        weekEventService.publicEventData(weekEventVo);
        return toAjax(1);
    }

    /**
     * 删除事件记录看版
     */
    @Log(title = "事件记录看版", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(weekEventService.deleteWeekEventByIds(ids));
    }

    /**
     * 上传事件记录看版列表
     */
    @PostMapping("/uploadExcel")
    public AjaxResult uploadWeekEventExcel(MultipartFile file){
    try {
        Workbook workbook=null;
        String filename = file.getOriginalFilename();
        if(filename.contains(".xlsx")) {
            workbook = new XSSFWorkbook(file.getInputStream());
        }else{
            workbook = new HSSFWorkbook(file.getInputStream());
        }
        weekEventService.uploadWeekEventExcel(workbook);
        return AjaxResult.success();
    }catch (Exception e){
        String resultMsg="上传失败";
        e.printStackTrace();
        String message = e.getMessage();
        return AjaxResult.error(resultMsg);
      }
    }
}
